package com.inossem.warehousestorage_inossem.activity.emergency_warehousing;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.emergency_warehousing.extra.EmergencyWarehousingExtra;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.emergencyWarehousing.EmergencyReceiveOperatorListAdapter;
import com.inossem.warehousestorage_inossem.bean.common.GetRoleBean;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingConfirmBean;
import com.inossem.warehousestorage_inossem.bean.emergencyWarehousing.EmergencyWarehousingReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.constant.TypeEnum;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.common.GetRoleRequest;
import com.inossem.warehousestorage_inossem.request.emergencyWarehousing.EmergencyWarehousingSubmitRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.EmergencyWarehousingUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * 作者：wsy on 2017/11/28 19:25
 * <p>
 * 邮箱：445979770@qq.com
 */

public class EmergencyWarehousingReceiveAddOperatorActivity extends BaseActivity {
    private static final String SUCCESS = "0";//成功
    private TextView title, nextStep;

//    private PullToRefreshLayout refresh;

    private PullableListView operatorList;

    private EmergencyReceiveOperatorListAdapter adapter;

//    private List<EmergencyWarehousingReceiveOverviewBean.DataBean.RoleListBean> mRoleList;

    private List<GetRoleBean.RoleBean> mRoleList;

    private String mRoleName ,mRoleCode ,  mReceipNumber , libraryStatusCode , emergencyTypeCode;

    private int isNew;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_emergency_warehousing_receive_add_operator);
        initView(EmergencyWarehousingReceiveAddOperatorActivity.this);

    }

    @Override
    protected void onResumeImpl() {
        initData(EmergencyWarehousingReceiveAddOperatorActivity.this);
    }
    private void getExtra(Activity activity) {
        mReceipNumber = activity.getIntent().getStringExtra(EmergencyWarehousingExtra.RECEIP_NUMBER);
        libraryStatusCode = activity.getIntent().getStringExtra(EmergencyWarehousingExtra.STATUS_CODE);
        emergencyTypeCode = activity.getIntent().getStringExtra(EmergencyWarehousingExtra.EMERGENCT_TYPE_CODE);
        isNew = Integer.parseInt(activity.getIntent().getStringExtra(EmergencyWarehousingExtra.IS_NEW));

    }
    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
//        refresh = (PullToRefreshLayout) activity.findViewById(R.id.operatorListRefresh);
        operatorList = (PullableListView) activity.findViewById(R.id.operatorList);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_emergency_warehousing_receive_add_operator_title);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (libraryStatusCode.equals("1")) {
                    if (EmergencyWarehousingUtils.haveAgentPerson(EmergencyWarehousingReceiveAddOperatorActivity.this)) {
                        submitRequest(EmergencyWarehousingReceiveAddOperatorActivity.this);
                    }else {
                        Toast.makeText(EmergencyWarehousingReceiveAddOperatorActivity.this , getResources().getString(R.string.activity_emergency_warehousing_receive_add_operator_please_add_the_agent) ,Toast.LENGTH_SHORT).show();
                    }
                }else {
                    if (isNew == 0) {
                        Toast.makeText(EmergencyWarehousingReceiveAddOperatorActivity.this , getResources().getString(R.string.activity_emergency_warehousing_receive_add_operator_cannot_submit) ,Toast.LENGTH_SHORT).show();
                    }else {
                        submitRequest(EmergencyWarehousingReceiveAddOperatorActivity.this);
                    }
                }
//                submitRequest(EmergencyWarehousingReceiveAddOperatorActivity.this);
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_emergency_warehousing_doverview,     new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        operatorList.setPullUpFlag(false);
        operatorList.setPullDownFlag(false);
        getExtra(EmergencyWarehousingReceiveAddOperatorActivity.this);
        getData(activity);
    }



    private void addReceiverListData(Context context, EmergencyWarehousingReceiveOverviewBean data, ListView listView) {
        adapter = new EmergencyReceiveOperatorListAdapter(context, data,mRoleList,mRoleName ,mRoleCode ,mReceipNumber, new EmergencyWarehousingReceiveAddOperatorActivity.EmergencyWarehousingReceiveAddOperatorListener() {
            @Override
            public void onDeleteOperatorListener() {
                initData(EmergencyWarehousingReceiveAddOperatorActivity.this);
            }

            @Override
            public void onSelectRole(String roleName, String roleCode) {
                mRoleName = roleName;
                mRoleCode = roleCode;
            }

        });
        listView.setAdapter(adapter);
    }
    private void getData(Context context) {
        if (mRoleList == null) {
            GetRoleRequest.request(context, TypeEnum.EMERGENCY_WAREHOUSING ,new GsonBeanListener<GetRoleBean>() {
                @Override
                public void onSuccess(GetRoleBean bean) {
                    mRoleList = bean.getBody();
                    EmergencyWarehousingReceiveOverviewBean data = EmergencyWarehousingUtils.getEmergencyWarehousingReceiveOverviewBean(context);
                    addReceiverListData(context, data, operatorList);
                }
            });
        } else {
            EmergencyWarehousingReceiveOverviewBean data = EmergencyWarehousingUtils.getEmergencyWarehousingReceiveOverviewBean(context);
            addReceiverListData(context, data, operatorList);
        }
    }
    private void submitRequest (Context context) {
//        Gson gson = new Gson();
//        LogUtils.i(gson.toJson(EmergencyWarehousingUtils.getEmergencyWarehousingSubmitBean(context , mReceipNumber )));
        EmergencyWarehousingSubmitRequest.request(EmergencyWarehousingReceiveAddOperatorActivity.this,EmergencyWarehousingUtils.getEmergencyWarehousingSubmitBean(context , mReceipNumber )
                , new GsonBeanListener<EmergencyWarehousingConfirmBean>() {
            @Override
            public void onSuccess(EmergencyWarehousingConfirmBean bean) {
                if (SUCCESS.equals(bean.getBody().getIs_success())) {
                    SPUtils.removeReceiveEmergencyWarehousingInformation(context);
                    Intent intent = new Intent(context, EmergencyWarehousingResultActivity.class);
                    intent.putExtra(InStorageExtra.MESSAGE, bean.getBody().getMessage());
                    intent.putExtra(InStorageExtra.CONFIRM_MESSAGE,context.getResources().getString(R.string.activity_receive_result_has_confirmed));
                    startActivity(intent);
                } else {
                    if (!TextUtils.isEmpty(bean.getBody().getMessage())) {
                        Utils.middleToast(context, bean.getBody().getMessage());
                    }
                }
            }
        });
    }
    public interface EmergencyWarehousingReceiveAddOperatorListener {
        void onDeleteOperatorListener();

        void onSelectRole(String roleName, String roleCode);

    }
}
